# 本文件负责管理不同平台并分配引脚
import os
try:
    from shared_types import PLATFORM_TYPES, MotorPinAssignment, ElectroMagneticPinAssignment
except ImportError:
    pass


class PinAssignment:
    def __init__(self):
        self.PLATFORM_TYPE: "PLATFORM_TYPES"
        if os.uname().machine.find("ESP32S3") != -1:
            self.PLATFORM_TYPE = "ESP32S3"
        elif os.uname().machine.find("ESP32") != -1:
            self.PLATFORM_TYPE = "ESP32"
        else:
            raise ValueError(f"Unsupported platform {os.uname()}")

    def get_electromagnet_pins(self) -> "ElectroMagneticPinAssignment":
        if self.PLATFORM_TYPE == "ESP32S3":
            return {
                "sw1": 13,
                "sw2": 12,
                "x595": {
                    "x595_clear": 11,
                    "x595_clock": 10,
                    "x595_latch": 9,
                    "x595_data": 8,
                }
            }
        elif self.PLATFORM_TYPE == "ESP32":
            return {
                "sw1": 27,
                "sw2": 25,
                "x595": {
                    "x595_clear": 26,
                    "x595_clock": 25,
                    "x595_data": 33,
                    "x595_latch": 32
                }
            }
        else:
            raise ValueError(f"Unsupported platform {self.PLATFORM_TYPE}")

    def get_motor_pins(self) -> "MotorPinAssignment":
        if self.PLATFORM_TYPE == "ESP32S3":
            return {
                "sw1": 10,
                "sw2": 11,
                "pwm": 12
            }
        elif self.PLATFORM_TYPE == "ESP32":
            return {
                "sw1": 27,
                "sw2": 25,
                "pwm": 22
            }
        else:
            raise ValueError(f"Unsupported platform {self.PLATFORM_TYPE}")

    def get_pin_assignment(self, platform_type: "PLATFORM_TYPES"):
        return {}

pin_assignments = PinAssignment()